package boun.swe.bob.service;

import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;


/**
 * This controller is used for message detail actions
 */
public class MessageDetailController extends BaseController {

         /**
     * Shows the page
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return page
     */
    @RequestMapping(method = RequestMethod.GET)
        public ModelAndView showPage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
            Map<String, Object> myModel = Maps.newHashMap();

            String messageId= httpServletRequest.getParameter("from_message_id");
        if(Strings.isNullOrEmpty(messageId) && httpServletRequest.getSession().getAttribute("from_message_id")!=null)
        {
               messageId=httpServletRequest.getSession().getAttribute("from_message_id").toString();
        }
        myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
            myModel.put("himself",  true  );

        httpServletRequest.getSession().setAttribute("from_message_id", messageId);
        httpServletRequest.getSession().setAttribute("toUserId", httpServletRequest.getParameter("toUserId"));

        if(null!=messageId)
        {
            myModel.put("from_message_id",messageId);
            myModel.put("userMessage", userMessageDao.findByPrimaryKey((Integer.parseInt(messageId))));
        }
            return new ModelAndView("messageDetail", "model", myModel);
        }

     /**
     * Handles HTTP POST action for sending a message
     *
     * @param httpServletRequest http request
     * @param httpServletResponse http response
     * @return the result message
     */
    @RequestMapping(method = RequestMethod.POST)
    public ModelAndView sendMessage(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        Map<String, Object> myModel = Maps.newHashMap();

        //String fromMsgId =httpServletRequest.getParameter("from_message_id");
         String fromMsgId=httpServletRequest.getSession().getAttribute("from_message_id").toString();
         String toUserId=httpServletRequest.getSession().getAttribute("toUserId").toString();
         String msgTitle = httpServletRequest.getParameter("msgTitle");
         String msgBody = httpServletRequest.getParameter("msgBody");
        //String toUserId = httpServletRequest.getParameter("toUserId");

         if(!Strings.isNullOrEmpty(fromMsgId) && //!Strings.isNullOrEmpty(msgTitle) && 
                 !Strings.isNullOrEmpty(msgBody) && !Strings.isNullOrEmpty(toUserId)) {
             userMessageDao.addNewSubMessage(Integer.parseInt(fromMsgId),getLoggedInUser(httpServletRequest).getUserId(),Integer.parseInt(toUserId),msgTitle,msgBody);
        }

         String messageId= httpServletRequest.getParameter("from_message_id");

            if(null != getLoggedInUser(httpServletRequest).getUserId()) {

                myModel.put("user", userDao.findByPrimaryKey(getLoggedInUser(httpServletRequest).getUserId()));
                myModel.put("himself",  true  );

            }
        messageId= httpServletRequest.getParameter("from_message_id");

        if(null!=messageId)
        {
            myModel.put("from_message_id",messageId);
            myModel.put("userMessage", userMessageDao.findByPrimaryKey((Integer.parseInt(messageId))));
        }
            return new ModelAndView("messageDetail", "model", myModel);
        }
}

